/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-06 09:51:29  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.bp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import com.google.gson.Gson;
import com.joysuch.wwyt.bp.bean.BpRegulationDocumentTypeAddBean;
import com.joysuch.wwyt.bp.bean.BpRegulationDocumentTypeEditBean;
import com.joysuch.wwyt.bp.bean.BpRegulationDocumentTypeListBean;
import com.joysuch.wwyt.bp.entity.BpRegulationDocumentType;
import com.joysuch.wwyt.bp.service.BpRegulationDocumentTypeService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 制度规范管理——文档类型
 */
@RestController
@Api(tags = "制度规范管理—文档类型")
@RequestMapping("/bp/regulation-document-type")
public class BpRegulationDocumentTypeController {

    private static final Logger logger = LoggerFactory.getLogger(BpRegulationDocumentTypeController.class);

    @Autowired
    private BpRegulationDocumentTypeService bpRegulationDocumentTypeService;

    /**
     * 添加文档类型
     * 
     * @param regDocTypeAdd
     *            待添加的文档类型相关属性[name]
     * @return
     */
    @RequestMapping("add")
    public Object add(@Valid BpRegulationDocumentTypeAddBean regDocTypeAdd) {
        try {
            if (bpRegulationDocumentTypeService.checkName(regDocTypeAdd.getName(), regDocTypeAdd.getDocCode())) {
                return ResultBean.fail(102, "文档类型名称重复");
            }
            bpRegulationDocumentTypeService.add(regDocTypeAdd);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    /**
     * 修改文档类型
     * 
     * @param regDocTypeEdit
     *            待修改的文档相关属性
     */
    @RequestMapping("update")
    public Object update(@Valid BpRegulationDocumentTypeEditBean regDocTypeEdit) {
        try {
            if (regDocTypeEdit.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            if (bpRegulationDocumentTypeService.checkName(regDocTypeEdit.getName(),regDocTypeEdit.getDocCode())) {
                return ResultBean.fail(103, "文档类型名称重复");
            }
            BpRegulationDocumentType temp = bpRegulationDocumentTypeService.findById(regDocTypeEdit.getId());
            if (temp != null && temp.getDocumentCount() > 0) {
                return ResultBean.fail(104, "该类型下存在文件，无法更新");
            }
            bpRegulationDocumentTypeService.update(regDocTypeEdit);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + regDocTypeEdit.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    @RequestMapping("page")
    public Object page(Pageable page, String docCode) {
        try {
            Page<BpRegulationDocumentType> data = bpRegulationDocumentTypeService.findByPage(page, docCode);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    /**
     * 获取所有文档类型列表
     */
    @RequestMapping("list")
    public Object listAll(String docCode) {
        try {
            List<BpRegulationDocumentTypeListBean> data = bpRegulationDocumentTypeService.findAll(docCode);
            return ResultBean.wrapSuccessfulResult(data);
        } catch (Exception e) {
            logger.error("查询列表失败", e);
            return ResultBean.fail(101, "查询列表失败");
        }
    }

    /**
     * 删除文档类型
     * 
     * @param id
     *            指定文档类型 ID
     */
    @RequestMapping("delete")
    public Object delete(Long id) {
        BpRegulationDocumentType temp = bpRegulationDocumentTypeService.findById(id);
        if (temp != null && temp.getDocumentCount() > 0) {
            return ResultBean.fail(102, "该类型下存在文件，无法删除");
        }
        bpRegulationDocumentTypeService.delete(id);
        return ResultBean.success("");
    }

    @RequestMapping("save")
    public Object save(String listJson, String docCode) {
        if (listJson == null) {
            return ResultBean.fail(102, "未传入参数");
        }
        Gson gson = new Gson();
        List<Map<String, Object>> docTypeList = gson.fromJson(listJson, ArrayList.class);
        bpRegulationDocumentTypeService.save(docTypeList, docCode);
        return ResultBean.success("");
    }
}
